package com.everflourish.yeah100.http;

import android.content.Context;

import com.android.volley.NoConnectionError;
import com.android.volley.TimeoutError;
import com.android.volley.VolleyError;
import com.everflourish.yeah100.ui.dialog.LoadDialog;
import com.everflourish.yeah100.utils.LogUtil;
import com.everflourish.yeah100.utils.MyToast;
import com.everflourish.yeah100.utils.Yeah100;

import java.util.HashMap;
import java.util.Map;

public class BaseRequest extends HttpURL {

    /**
     * The default socket timeout in milliseconds 60000
     */
    public static final int DEFAULT_TIMEOUT_MS = 1000 * 20;
    public static final int DEFAULT_TIMEOUT_MS10 = 1000 * 10;
    public static final int DEFAULT_TIMEOUT_MS60 = 1000 * 60;
    public static final int DEFAULT_TIMEOUT_MS_2 = 1000 * 15;
    public static final int DEFAULT_TIMEOUT_9S = 1000 * 9;

    /**
     * The default number of retries
     */
    public static final int DEFAULT_MAX_RETRIES = 3;
    public static final int DEFAULT_MAX_RETRIES1 = 1;

    /**
     * The default backoff multiplier
     */
    public static final float DEFAULT_BACKOFF_MULT = 0f;

    // true则添加请求头tokenId,false则相反
    private boolean mIsAddHeader = true;

    /**
     * 添加请求头tokenId
     *
     * @return map
     */
    public Map<String, String> addRequestHeader() {
        Map<String, String> map = new HashMap<String, String>();
        if (mIsAddHeader && Yeah100.loginInfo.tokenId != null) {
            map.put("tokenId", Yeah100.loginInfo.tokenId);
            LogUtil.d("------->" + Yeah100.loginInfo.tokenId);
        }
        return map;
    }

    /**
     * 处理错误请求
     *
     * @param context  上下文
     * @param error    错误对象
     * @param errorMsg 错误内容
     * @return -1表示处理失败 1表示处理成功
     */
    public int disposeError(Context context, LoadDialog loadDialog,
                            VolleyError error, String errorMsg) {
        LogUtil.e(errorMsg, error);
        LoadDialog.dismiss(loadDialog);
        if (error instanceof TimeoutError) {//请求超时
            MyToast.showShort(context, "网络不给力哦~~~");
        } else if (error instanceof NoConnectionError) {//没有网络
            MyToast.showShort(context, "网络已断开");
        } else {
            MyToast.showShort(context, errorMsg);
        }
        return 1;
    }
}